package com.ybwh.springboot2.sharding5.service;

import com.xfvape.uid.UidGenerator;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashSet;
import java.util.Set;

/**
 * <p>
 * 百度雪花算法生成器
 * </p>
 *
 * @author fanbeibei
 * @version 1.0.0
 * @date 2023/7/25 11:30
 */
@Service
public class BaiduUidService {
    @Resource
    private UidGenerator uidGenerator;

    public Long getSnowFlakeId() {
        return uidGenerator.getUID();
    }

    public Set<Long> batchGetSnowFlakeId(final int batch) {
        Set<Long> idList = new HashSet<>(batch);

        for (int i = 0; i < batch; i++) {
            // 这里是从本地缓存里取id，所以不会有性能问题
            idList.add(uidGenerator.getUID());
        }

        return idList;
    }
}
